//
//  FileCacheManager.swift
//  SFDevFly
//
//  Created by shen on 2023/9/28.
//

import Foundation

private let FILE_CACHE_CONFIG_KEY = "FILE_CACHE_CONFIG_KEY"
private let JSON_CACHE_KEY = "JSON_CACHE_KEY"

let kCacheManager = FileCacheManager.shared

class FileCacheManager {
    static let shared: FileCacheManager = .init()

    private init() {
        let fileDic = UserDefaults.standard.object(forKey: FILE_CACHE_CONFIG_KEY) as? [String: String]
        file = File(cacheConfig: fileDic)

        jsonString = UserDefaults.standard.string(forKey: JSON_CACHE_KEY) ?? ""
        print("Cache JSON => \(jsonString)")
    }

    var file: File
    func asyncConfigSave() {
        UserDefaults.standard.set(file.toCacheConfig(), forKey: FILE_CACHE_CONFIG_KEY)
        UserDefaults.standard.synchronize()
    }

    var jsonString: String
    func asyncJsonSave() {
        UserDefaults.standard.set(jsonString, forKey: JSON_CACHE_KEY)
        UserDefaults.standard.synchronize()
    }
}
